<?php

namespace app\common\taglib;

use think\facade\Env;
use think\template\TagLib;

class Ns extends TagLib
{
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'tinfo'  => ['attr' => 'db,where,id', 'close' => 1],
        'tfield' => ['attr' => 'db,where,name', 'close' => 0],
        'clist'  => ['attr' => 'db,order,limit,where,id,key', 'close' => 1],
        'tlist'  => ['attr' => 'db,order,limit,where,id,key', 'close' => 1],
        'nav'    => ['attr' => 'catid,type,self,key,id', 'close' => 1],
        'block'  => ['attr' => 'blockid,pos,key,id', 'close' => 0],
        'slide'  => ['attr' => 'flashid,key,mod,id', 'close' => 0],
        //'pre'  => ['attr' => 'blank,msg', 'close' => 0],
        //'next' => ['attr' => 'blank,msg', 'close' => 0],
    ];
    
    /**
     * 根据条件查询字段
     *
     * @param $tag
     * @return string
     */
    public function tagTfield($tag)
    {
        $db = $tag['db']; //要查询的数据表
        $where = isset($tag['where']) ? $tag['where'] : '';//查询条件
        $name = $tag['name'];
        $str = '<?php ';
        $str .= 'echo db("' . $db . '")->where("' . $where . '")->value("' . $name . '");';
        $str .= '?>';
        return $str;
    }
    
    /**
     * 根据条件查询一条数据
     *
     * @param $attr
     * @param $content
     * @return string
     */
    public function tagTinfo($attr, $content)
    {
        $db = $attr['db']; //要查询的数据表
        $where = isset($attr['where']) ? $attr['where'] : '';; //查询条件
        $id = $attr['id'];
        $str = '<?php ';
        $str .= '$' . $id . ' =db("' . $db . '")->where("' . $where . '")->find();';
        $str .= '?>';
        $str .= $content;
        return $str;
    }
    
    public function tagClist($attr, $content)
    {
        $db = $attr['db']; //要查询的数据表
        $order = isset($attr['order']) ? $attr['order'] : ' a.sort asc,a.create_time desc,a.id desc';    //排序
        $limit = isset($attr['limit']) ? $attr['limit'] : '15'; //多少条数据
        $where = isset($attr['where']) ? $attr['where'] . ' and (status = 1 or (status = 0 and create_time <' . time() . '))' : ' status = 1 or (status = 0 and create_time <' . time() . ') '; //查询条件
        $id = $attr['id'];
        $key = isset($attr['key']) ? $attr['key'] : 'k';
        $str = '<?php ';
        $str .= '$result = db("' . $db . '")->alias("a")->join("category c"," a.catid = c.id","left")
            ->where("' . $where . '")
            ->field("a.*,c.catdir,c.catname")
            ->limit(' . $limit . ')
            ->order("' . $order . '")
            ->select();';
        $str .= 'if($result){';
        $str .= 'foreach ($result as $' . $key . '=>$' . $id . '):';
        $str .= '$result[$' . $key . ']["time"]= toDate($' . $id . '["create_time"],"Y-m-d");';
        $str .= '$result[$' . $key . ']["thumb"]= $' . $id . '["thumb"]?$' . $id . '["thumb"]:"";';
        $str .= '?>';
        $str .= '<?php endforeach; ?>';
        $str .= '<?php ';
        $str .= 'foreach ($result as $' . $key . '=>$' . $id . '):';
        $str .= '?>';
        $str .= $content;
        $str .= '<?php endforeach; ?>';
        $str .= '<?php }else{echo "<div class=\'fly-none\'>没有相关数据</div>";}?>';
        /*$str .= '<?php dump($result);?>';*/
        return $str;
    }
    
    public function tagTlist($attr, $content)
    {
        $db = $attr['db']; //要查询的数据表
        $order = isset($attr['order']) ? $attr['order'] : 'id desc';    //排序
        $limit = isset($attr['limit']) ? $attr['limit'] : 10; //多少条数据
        $where = isset($attr['where']) ? $attr['where'] : 1; //查询条件
        $orderRaw = isset($attr['orderRaw']) ? $attr['orderRaw'] : 'id desc'; //查询条件
        $id = $attr['id'];
        $key = isset($attr['key']) ? $attr['key'] : 'k';
        $str = '<?php ';
        $str .= '$result = db("' . $db . '")->where("' . $where . '")->orderRaw("' . $orderRaw . '")->limit(' . $limit . ')->order("' . $order . '")->select();';
        $str .= 'foreach ($result as $' . $key . '=>$' . $id . '):';
        $str .= '$result[$' . $key . ']["time"]= isset($' . $id . '["create_time"])?toDate($' . $id . '["create_time"]):"";';
        $str .= '?>';
        $str .= $content;
        $str .= '<?php endforeach?>';
        return $str;
    }
    
    //轮播图
    public function tagSlide($attr, $content)
    {
        $sid = !empty($attr['sid']) ? $attr['sid'] : '';
        $slide = db('slide')->where("status=1 and id=$sid")->find();
        if (empty($slide)) {
            return '';
        }
        
        $filepath = Env::get('app_path') . strtolower(config('default_module')) . '/view/';
        
        $limit = $slide['num'] ? $slide['num'] : 5;
        $sql = "db('slide_data')->where(\"status=1 and sid=$sid\")->order(\"sort ASC,id DESC \")->limit(\"{$limit}\")->select()";
        $str = '<?php ';
        $str .= '$_result=' . $sql . ';if ($_result): $i=0;';
        $str .= 'foreach($_result as $k=>$r):';
        $str .= '$i++;?>';
        $end = '<?php endforeach; endif;?>';
        $Tpl = $filepath . $slide['template'] . '.html';
        $html = file_get_contents($Tpl);
        $html = str_replace(['{loop}', '{/loop}', '{$width}', '{$height}', '{$sid}'], [$str, $end, $slide['width'], $slide['height'], $sid], $html);
        return $html;
    }
    
    //局部
    public function tagBlock($attr, $content)
    {
        $bid = !empty($attr['bid']) ? $attr['bid'] : '';
        $r = db('block')->find($bid);
        return $r['content'];
    }
    
    //导航
    public function tagNav($attr, $content)
    {
        $id = !empty($attr['id']) ? $attr['id'] : 'r';
        $key = !empty($attr['key']) ? $attr['key'] : 'i';
        $catId = !empty($attr['catid']) ? $attr['catid'] : '0';
        $str = '';
        $str .= '<?php $result = cache("Category");$' . $key . '=0;';
        $str .= 'foreach($result as $key=>$' . $id . '):';
        $str .= 'if($' . $id . '[\'ismenu\']==1 && intval(' . $catId . ')==$' . $id . '["parentid"]) :';
        $str .= '++$' . $key . ';?>';
        $str .= $content;
        $str .= '<?php endif;?>';
        $str .= '<?php endforeach;?>';
        return $str;
    }
}